package com.zhq.inter.dao;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.zhq.inter.UserInter;
import com.zhq.pojo.User;

public class Dao {
	public static void getUserList(String userName) {

		String resource = "com/zhq/map/MyBatisConfig.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sqlSessionFactory.openSession();

		try {
			UserInter userOperation = session.getMapper(UserInter.class);
			List<User> users = userOperation.selectUsers(userName);
			for (User user : users) {
				System.out.println(user.getId() + ":" + user.getUserName());
			}

		} finally {
			session.close();
		}
	}

	public static void addUser() {

		String resource = "com/zhq/map/MyBatisConfig.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sqlSessionFactory.openSession();

		User user = new User();
		user.setUserName("飞鸟");
		user.setPassword("123");
		try {
			UserInter userOperation = session.getMapper(UserInter.class);
			userOperation.addUser(user);
			session.commit();
			System.out.println("当前增加的用户 id为:" + user.getId());
		} finally {
			session.close();
		}
	}
	
	public static void updateUser(){
		String resource = "com/zhq/map/MyBatisConfig.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sqlSessionFactory.openSession();
		
        //先得到用户,然后修改，提交。
        try {
            UserInter userOperation=session.getMapper(UserInter.class);
            User user = userOperation.findById(4);            
            user.setPassword("123456");
            userOperation.updateUser(user);
            session.commit();
            
        } finally {
            session.close();
        }
    }
	
	/**
     * 删除数据，删除一定要 commit.
     * @param id
     */
    public static void deleteUser(int id){
    	String resource = "com/zhq/map/MyBatisConfig.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sqlSessionFactory.openSession();
		
        try {
            UserInter userOperation=session.getMapper(UserInter.class);          
            userOperation.deleteUser(id);
            System.out.println("delete success");
            session.commit();            
        } finally {
            session.close();
        }
    }
}
